package csu.rjxyg.util.impl;

import java.net.UnknownHostException;

import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoOptions;

import csu.rjxyg.util.DBUtil;


//数据库工具类
public class MongoDBUtil implements DBUtil{
	private static Mongo mongo = null;
	
	private static String DBString = "rjxygDB";//数据库名
	
//	private static String hostName = "10.4.4.130";//主机名
//	private static int port = 25138;//端口号
	private static String hostName = "localhost";//主机名
	private static int port = 27017;//端口号
	private static int poolSize = 10;//连接池大小
    
    static{
        System.out.println(hostName);
        System.out.println(port);
    }
	
	//获取数据库连接
	public Object getConnection(){
		if(mongo == null){
			init();
		}
		
		return mongo.getDB(DBString);
	}
	
	
	//初始化数据库
	private void init(){
		try {
			mongo = new Mongo(hostName, port);
			MongoOptions opt = mongo.getMongoOptions();
			opt.connectionsPerHost = poolSize;
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}